﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Table=FCNS.Data.Table;
using System.Diagnostics;
using System.Data;
using DoubleH.Utility;
using System.Collections.ObjectModel;

namespace PayS
{
    /// <summary>
    /// ShouKuan.xaml 的交互逻辑
    /// </summary>
    public partial class ShouKuan : Window
    {
        public ShouKuan()
        {
            InitializeComponent();

            InitEvent();
        }

        double selectMoney = 0;
        DataTableText TableText;
        ObservableCollection<Table.PayS> payS;

        public void Init(DataTableText tableText, Table.PayS obj)
        {
            dataGridObject.ShowHeaderMenu = false;
            TableText = tableText;
            uCUniqueSBank.Init(Table.UniqueS.EnumFlag.支付方式);
            dataGridObject.InitColumns(DataTableText.可收支单据);

            if (obj == null)
                SelectedCustomerS(-1);
            else
            {
                SelectedCustomerS(obj.CorSId);
                uCUniqueSBank.SelectedObjectId = obj.BankId;
            }
            label1.Content = this.Title = tableText.ToString();
        }

        private void GetBill()
        {
          payS = Table.PayS.GetBill(customerSinfo1.SelectedObjectId, TableText == DataTableText.收款单 ? Table.PayS.EnumFlag.收款单 : Table.PayS.EnumFlag.付款单);

          dataGridObject.ItemsSource = payS;
            double d = 0;
            foreach (Table.PayS row in payS)
            {
                row.RealMoney = row.Money;
                d += row.Money;
            }

            label1Due.Content = Math.Round(d,Table.SysConfig.SysConfigParams.DecimalPlaces);
            ckbSelectedAll_Checked(null, null);
            buttonOK.IsEnabled = dataGridObject.HasItems;
        }

        private void SelectedCustomerS(Int64 corSId)
        {
            customerSinfo1.SelectedObjectId = corSId;
            GetBill();
        }

        private void InitEvent()
        {
            buttonOK.Click += (ss, ee) => Save(); 
            customerSinfo1.CorSChanged += (ee) => GetBill(); 
            dataGridObject.CellEditEnding += (ss, ee) => CheckCellEditValue(ee);
        }

        private void Save()
        {
            if (uCUniqueSBank.SelectedObjectId == -1)
            {
                MessageWindow.Show("请选择支付方式");
                return;
            }

            foreach (Table.PayS p in payS)
            {
                if (!p._Pay)
                    continue;

                p.OrderDateTime = DateTime.Now;
                if (!string.IsNullOrEmpty(p.Invoice))
                    p.UseInvoice = true;

                p.BankId = uCUniqueSBank.SelectedObjectId;
               p.Voucher= textBoxVoucher.Text;
                p.ShenHe();
            }
            this.Close();
        }

        private void ckbSelectedAll_Checked(object sender, RoutedEventArgs e)
        {
            if (label1Due.Content == labelMoney.Content)
                return;

            selectMoney = (double)label1Due.Content;
            labelMoney.Content = selectMoney;
            foreach (Table.PayS p in payS)
                p._Pay = true;
        }

        private void ckbSelectedAll_Unchecked(object sender, RoutedEventArgs e)
        {
            if (0 == (double)labelMoney.Content)
                return;

            selectMoney = 0;
            labelMoney.Content = selectMoney;
            foreach (Table.PayS p in payS)
                p._Pay = false;
        }

        private void CheckCellEditValue(DataGridCellEditEndingEventArgs ee)
        {
            DataGridTextColumn textColumn = ee.Column as DataGridTextColumn;
            if (textColumn == null)
                return;

                string binding = ((Binding)textColumn.Binding).Path.Path;
                double d = 0;
                switch (binding)
                {
                    case "RealMoney":
                        TextBox textBox = (TextBox)ee.EditingElement;
                        if (!double.TryParse(textBox.Text, out d))
                            textBox.Undo();
                        else
                            if (d < 0)
                                textBox.Undo();

                        break;
                }
        }
    }
}
